/********************************/
/* analysis of treatment effect */
/********************************/

// read in data
clear all
insheet using parseddata.csv, comma

// allow scrolling without user input
set more 1

// unique subject ids
gen uniqueid = treatment*100 + session*20 + subjectid

// normalized price
gen normprice = price*(price>=0.5) + (1-price)*(price <0.5)
gen highprice = price>0.5
gen pricenotonehalf = price~=0.5

gen nocontradiction = contradiction==0

// create categorical variables for creating tables

gen detailbehavior = (notrade == 1)
replace detailbehavior = 2 if rational == 1
replace detailbehavior = 3 if herding == 1
replace detailbehavior = 4 if contrarian == 1
replace detailbehavior = 5 if partialherding == 1
replace detailbehavior = 6 if partialcontrarian == 1
gen detailirrational = (detailbehavior==0)

// weak versions of herding and contrarian
gen weakherding = herding | partialherding
gen weakcontrarian = contrarian | partialcontrarian

// data by treatment
bysort treatment: tab detailbehavior
 
// treatment comparisons
//ttest detailirrational, by(treatment)
//ttest notrade, by(treatment)
//ttest rational, by(treatment)
//ttest herding, by(treatment)
//ttest contrarian, by(treatment)
//ttest partialherding, by(treatment)
//ttest partialcontrarian, by(treatment)

// testing symmetry
//logit notrade highprice if pricenotonehalf==1, cluster(uniqueid)
//logit rational highprice if pricenotonehalf==1, cluster(uniqueid)
//logit herding highprice if pricenotonehalf==1, cluster(uniqueid)
//mfx
//logit contrarian highprice if pricenotonehalf==1, cluster(uniqueid)
//mfx
//logit other highprice if pricenotonehalf==1, cluster(uniqueid)

// detailed data within treatment
tab detailbehavior normprice if treatment == 0, col
tab detailbehavior normprice if treatment == 1, col
//tab detailbehavior normprice if treatment == 1 & nocontradiction==1, col
//tab detailbehavior normprice if treatment == 1 & nocontradiction==0, col

// aggregate within treatment
logit notrade normprice if treatment == 0, cluster(uniqueid)
logit notrade normprice if treatment == 1, cluster(uniqueid)
logit rational normprice if treatment == 0, cluster(uniqueid)
logit rational normprice if treatment == 1, cluster(uniqueid)
logit herding normprice if treatment == 0 & normprice >0.51, cluster(uniqueid)
logit herding normprice if treatment == 1 & normprice >0.51, cluster(uniqueid)
logit contrarian normprice if treatment == 0 & normprice >0.51, cluster(uniqueid)
logit contrarian normprice if treatment == 1 & normprice >0.51, cluster(uniqueid)

// tests for path-dependence
// impact of no contradiction on herding
xi: logit herding i.normprice nocontradiction if treatment==1 & normprice>0.51, cluster(uniqueid)
xi: logit weakherding i.normprice nocontradiction if treatment == 1 & normprice>0.51, cluster(uniqueid)
xi: logit herding i.normprice nocontradiction if treatment==0 & normprice>0.51, cluster(uniqueid)
xi: logit weakherding i.normprice nocontradiction if treatment == 0 & normprice>0.51, cluster(uniqueid)

// impact of recent streak on herding
gen recentstreak = 0
replace recentstreak=1 if (streak>1 | nocontradiction)

xi: logit herding i.normprice recentstreak if treatment == 1 & normprice>0.51, cluster(uniqueid)
xi: logit weakherding i.normprice recentstreak if treatment == 1 & normprice>0.51, cluster(uniqueid)
xi: logit herding i.normprice recentstreak if treatment == 0 & normprice>0.51, cluster(uniqueid)
xi: logit weakherding i.normprice recentstreak if treatment == 0 & normprice>0.51, cluster(uniqueid)

// impact of no contradiction on contrarian
xi: logit contrarian i.normprice nocontradiction if treatment==1 & normprice>0.51, cluster(uniqueid)
mfx
xi: logit weakcontrarian i.normprice nocontradiction if treatment==1 & normprice>0.51, cluster(uniqueid)
mfx
xi: logit contrarian i.normprice nocontradiction if treatment==0 & normprice>0.51, cluster(uniqueid)
mfx
xi: logit weakcontrarian i.normprice nocontradiction if treatment==0 & normprice>0.51, cluster(uniqueid)
mfx

// impact of recent streak on contrarian
xi: logit contrarian i.normprice recentstreak if treatment==1 & normprice>0.51, cluster(uniqueid)
mfx
xi: logit weakcontrarian i.normprice recentstreak if treatment==1 & normprice>0.51, cluster(uniqueid)
mfx
xi: logit contrarian i.normprice recentstreak if treatment==0 & normprice>0.51, cluster(uniqueid)
mfx
xi: logit weakcontrarian i.normprice recentstreak if treatment==0 & normprice>0.51, cluster(uniqueid)
mfx

// tests for dependence across rounds
gen buy1 = action1==1
gen sell1 = action1==-1
gen buy0 = action0 == 1
gen sell0 = action0 == -1

logit buy1 lastassetval price, cluster(uniqueid)
logit buy0 lastassetval price, cluster(uniqueid)
logit sell1 lastassetval price, cluster(uniqueid)
logit sell0 lastassetval price, cluster(uniqueid)
logit buy1 assetval11 price, cluster(uniqueid)
logit buy0 assetval11 price, cluster(uniqueid)
logit sell1 assetval11 price, cluster(uniqueid)
logit sell0 assetval11 price, cluster(uniqueid)
logit buy1 assetval00 price, cluster(uniqueid)
logit buy0 assetval00 price, cluster(uniqueid)
logit sell1 assetval00 price, cluster(uniqueid)
logit sell0 assetval00 price, cluster(uniqueid)
logit buy1 assetval111 price, cluster(uniqueid)
logit buy0 assetval111 price, cluster(uniqueid)
logit sell1 assetval111 price, cluster(uniqueid)
logit sell0 assetval111 price, cluster(uniqueid)
logit buy1 assetval000 price, cluster(uniqueid)
logit buy0 assetval000 price, cluster(uniqueid)
logit sell1 assetval000 price, cluster(uniqueid)
logit sell0 assetval000 price, cluster(uniqueid)

// treatment comparisons
// collapse data by individual

collapse treatment detailirrational herding contrarian notrade rational partialherding partialcontrarian, by(uniqueid)
ranksum detailirrational, by(treatment)
ranksum herding, by(treatment)
ranksum contrarian, by(treatment)
ranksum notrade, by(treatment)
ranksum rational, by(treatment)
ranksum partialherding, by(treatment)
ranksum partialcontrarian, by(treatment)



